package com.change.core.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.change.core.web.request.AccountContext;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Integer userId = AccountContext.getAccountId();
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "creatorId", () -> userId, Integer.class);
        this.strictInsertFill(metaObject, "updateId", () -> userId, Integer.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Integer userId = AccountContext.getAccountId();
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateId", () -> userId, Integer.class);
    }
}
